layui.config({
    base: config.base + 'layui-admin/'
}).extend({
    index: 'lib/index'
}).use(['index'], function () {
    const $ = layui.$;
    const admin = layui.admin;
    const form = layui.form;
    const layer = layui.layer;
    const element = layui.element;
    form.verify({
        pass: [
            /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{6,36}$/
            , '密码需为6-36位数字、大写字母、小写字母、符号等4种类型任选3种的组合，不含空格！'
        ]
    });
    const fn = {
        //请求左侧导航
        side: function () {
            admin.req({
                url: '../../admin/menu/side.json',
                done: function (data) {
                    fn.tree(data.data);
                }
            });
            admin.req({
                url: '../../admin/notice/list.json',
                data: {statusId: 0, page: 1, offset: 0, limit: 10},
                done: function (data) {
                    if (data.total > 0) {
                        $('#notice-unread').addClass('layui-badge').text(data.total > 99 ? '99+' : data.total);
                    }
                }
            });
        }
        //根据返回的数据生成左侧导航
        , tree: function (data) {
            const side = [];
            layui.each(data, function (index, item) {
                side.push('<li data-name="nav-item' + item.id + '" class="layui-nav-item' + (index === 0 ? " layui-nav-itemed" : "") + '">');
                side.push('  <a href="javascript:;" lay-tips="' + item.title + '">');
                side.push('    <i class="' + item.iconCls + '"></i>');
                side.push('    <cite>' + item.title + '</cite>');
                side.push('  </a>');
                side.push('  <dl class="layui-nav-child">');
                layui.each(item.children, function (index, item) {
                    side.push('<dd data-name="nav-item' + item.id + '">');
                    side.push('  <a lay-href="../..' + item.address + '">');
                    side.push('    <i class="' + item.iconCls + '"></i>');
                    side.push('    <cite>' + item.title + '</cite>');
                    side.push('  </a>');
                    side.push('</dd>');
                });
                side.push('  </dl>');
                side.push('</li>');
            });
            $('#LAY-system-side-menu').html(side.join(''));
            element.render('nav', 'layadmin-system-side-menu');
        }
        //修改密码
        , pwd: function (data, fn) {
            admin.req({
                url: '../../admin/user/password.json'
                , contentType: "application/json;charset=UTF-8"
                , type: 'post'
                , data: JSON.stringify(data)
                , done: function (data) {
                    typeof fn === 'function' && fn();
                }
            });
        }
        //打开修改密码窗口
        , open: function () {
            admin.popup({
                type: 1
                , title: '修改密码'
                , area: '100'
                , content: $('#password-form')
                , btn: ['确定', '取消']
                , yes: function (index) {
                    //绑定用户编辑数据提交
                    form.on('submit(password-submit)', function (data) {
                        fn.pwd(data.field, function () {
                            layer.close(index);
                        });
                    });
                    $('#password-submit').trigger('click');
                }
            });
        }
        //锁屏
        , lock: function () {
            layui.data('lockScreen', {key: 'lockStatus', value: true});
            layer.open({
                type: 1
                , title: '锁屏'
                , area: '100'
                , shade: 0.8
                , closeBtn: 0
                , skin: 'layui-layer-admin'
                , content: $('#lock-form')
                , btn: ['解锁', '退出']
                , yes: function (index) {
                    fn.unlock($('#lockPassword').val(), function () {
                        layui.data('lockScreen', {key: 'lockStatus', value: false});
                        layer.close(index);
                    })
                }
                , btn2: function () {
                    fn.logout();
                    return false;
                }
            });
        }
        //解锁
        , unlock: function (lockPassword, fn) {
            admin.req({
                url: '../../admin/user/unlock.json'
                , data: {lockPassword: lockPassword}
                , done: function (data) {
                    typeof fn === 'function' && fn();
                }
            });
        }
        //退出
        , logout: function () {
            layer.confirm('是否退出系统？', {
                title: '退出系统'
                , icon: 3
                , closeBtn: 0
                , skin: 'layui-layer-admin'
            }, function (index) {
                location.href = '../../views/admin/logout.html';
                layer.close(index);
            });
        }
        , avatar: function () {
            admin.popup({
                type: 2
                , anim: -1
                , id: 'index-avatar'
                , skin: 'layadmin-note layui-anim layui-anim-upbit'
                , title: '修改头像'
                , area: ['460px', '350px']
                , shadeClose: true
                , content: config.base + 'views/admin/avatar.html'
            });
        }
        , load: function () {
            fn.side();
            if (layui.data('lockScreen').lockStatus) {
                fn.lock();
            }
        }
    };
    $('#edit-avatar').on('click', function () {
        fn.avatar();
    });
    $('#edit-password').on('click', function () {
        $('#password-form')[0].reset();//清空表单
        fn.open();
    });
    $('#lock-screen').on('click', function () {
        $('#lock-form')[0].reset();//清空表单
        fn.lock();
    });
    $('#logout').on('click', function () {
        fn.logout();
    });
    fn.load();
});